//
//  logTableViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/8/9.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "NewlogTableViewController.h"
#import "logTableViewController.h"
#import "OAUnDoneListCell.h"
#import "UnDoneModel.h"
#import "logxiangqingTableViewController.h"
@interface logTableViewController ()
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,assign)int totalCounts; //总条数
@property(nonatomic,assign)int currentPage; //当前页
@property (nonatomic,strong)NSMutableArray *mutArray;

@end

@implementation logTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的日志";
          __weak typeof(self) weakSelf = self;
     [self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
[weakSelf loadData];
 

    }];
    [weakSelf.tableView headerBeginRefreshing];
    
    [weakSelf.tableView addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
  
[weakSelf loadMore];
 
        }else{
        
            [self.tableView footerEndRefreshing];
            [self.view makeToast:@"没有更多了"];
    
        }
       
    }];
   UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
        self.navigationItem.rightBarButtonItem = rightIem;
   

    
    
}
-(void)searchAction{
    NewlogTableViewController *vc = [[NewlogTableViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark 加载网络数据
-(void)loadData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];
           [HttpTools requestWithURL:@"exchdialylog"
     arrays:array method:@"dailylogAllPage" success:^(id JSON) {
     // NSLog(@"-------%@",JSON);
_totalCounts = (int)JSON[@"number"];
        if (self.currentPage ==1) {
            [self.dataArray removeAllObjects];
        }
       NSArray *array = JSON[@"data"];
       _dataArray = [NSMutableArray array];
       for (NSDictionary *dic in array) {
    UnDoneModel * model = [[UnDoneModel alloc] init];
model.time = dic[@"postTime"];
model.title = dic[@"newsContent"];
model.fileId = dic[@"id"];
[_dataArray addObject:model];
    
}

        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
        if (self.dataArray.count ==0) {
        [self.view makeToast:[NSString stringWithFormat:@"暂时还没有工作日志哦"]];
        }
    } failure:^(NSError *error) {
     [self.tableView headerEndRefreshing];
        
    }];
 
}
- (void)loadMore{


}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


 UnDoneModel * model = self.dataArray[indexPath.row];
    CGFloat  titleFloat  = [OAToolBox sizebyText:model.title font:14 minsWidth:60];
    NSString * desstring = [NSString stringWithFormat:@"%@/%@/%@",model.time,model.state,model.name];
    CGFloat desFloat = [OAToolBox sizebyText:desstring font:14 minsWidth:80];
    return  titleFloat + desFloat + 20;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       OAUnDoneListCell * cell = [OAUnDoneListCell cellWithTableView:tableView];
    UnDoneModel * model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UnDoneModel * model = self.dataArray[indexPath.row];
     logxiangqingTableViewController * detailVC = [[logxiangqingTableViewController alloc]init];
detailVC.field = model.fileId;
    
        [self.navigationController pushViewController:detailVC animated:YES];
//        }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

@end
